$(function () {
    changeImgs();
});
function submits(data) {
    const param = {
        userLoginName: data.username,
        userPassword: data.password,
        captcha: data.captcha
    };

    $.postJSON(dl_coder_url.login, param , function (result) {
        const code = result.code;
        if('200'===code){
            $.cookie('loginAuth', result.data, { expires: 0.5, path: '/' });
            $(window).attr('location','index.html');
        }else {
            layer.msg(result.msg, {icon: 2,time: 3000});
            changeImgs();
        }

    })
}

function changeImgs(){
    const img = document.getElementById("admin-captcha");
    img.src = "system-api/auth/verification?"+Math.random();
    return true;
}

layui.use(['form'], function () {
    var form = layui.form,
        layer = layui.layer;

    // 登录过期的时候，跳出ifram框架
    if (top.location !== self.location) top.location = self.location;

    // 进行登录操作
    form.on('submit(login)', function (data) {
        data = data.field;
        if (data.username === '') {
            layer.msg('用户名不能为空');
            return false;
        }
        if (data.password === '') {
            layer.msg('密码不能为空');
            return false;
        }
        if (data.captcha === '') {
            layer.msg('验证码不能为空');
            return false;
        }
        submits(data);

        return false;
    });
});
